namespace Guia.Elements
{
    using System.Windows.Automation;
    using Guia.Elements.PatternServants;
    using Guia.Locator;

    /// <summary>
    /// This class represents a ComboBoxItem.
    /// </summary>
    public class ComboBoxItem : UiElement, ISelectionItemSingle
    {
        /// <summary>
        /// Represents a helper-servant for the SelectionItem-pattern.
        /// </summary>
        protected readonly ISelectionItemMulti SelectionItemServant;

        /// <summary>
        /// Initializes a new instance of the ComboBoxItem class. 
        /// </summary>
        /// <param name="automationElement">The AutomationElement representating the ComboBoxItem.</param>
        public ComboBoxItem(AutomationElement automationElement)
            : base(automationElement)
        {
            this.SelectionItemServant = new SelectionItemImpl(this);
        }

        /// <summary>
        /// Gets the default-condition to find a control by its type.
        /// </summary>
        public static SearchCondition ControlTypeCondition
        {
            get { return SearchCondition.ByControlType(ControlType.ListItem); }
        }

        /// <summary>
        /// Gets or sets a value indicating whether this ComboBoxItem is selected or not.
        /// </summary>
        public bool IsSelected
        {
            get
            {
                return this.SelectionItemServant.IsSelected;
            }

            set
            {
                this.SelectionItemServant.IsSelected = value;
            }
        }

        /// <summary>
        /// Gets the string-value of the item.
        /// </summary>
        public string TextValue
        {
            get
            {
                return this.Name;
            }
        }

        /// <summary>
        /// Selects the Item.
        /// If multiple selection is allowed, this selection causes that only this item is selected. Thus, other items may be unselected.
        /// </summary>
        public void Select()
        {
            this.SelectionItemServant.Select();
        }

        /// <summary>
        /// Unselects the item.
        /// </summary>
        public void Unselect()
        {
            this.SelectionItemServant.Unselect();
        }
    }
}